/**
 *
 * 请求学习词汇列表
 *
 */
package com.ilovetolearn.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.serialization.json.JSON;
	import com.ilovetolearn.business.InitializeWordDelegate;
	import com.ilovetolearn.model.ModelLocator;
	import com.ilovetolearn.vo.SentenceVo;
	import com.ilovetolearn.vo.WordVo;

	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class InitializeWordCommand implements ICommand, IResponder
	{
		private var _model:ModelLocator=ModelLocator.getInstance();
		private var delegate:InitializeWordDelegate;

		public function InitializeWordCommand()
		{
			delegate=new InitializeWordDelegate(this);
		}

		public function execute(event:CairngormEvent):void
		{
			delegate.initializeWord(event);
		}

		public function result(data:Object):void
		{
			var wordData:String=String(ResultEvent(data).result);
			var json:Object=new Object();
			json=JSON.decode(wordData);
			trace(wordData);
			var resultcode:String=json.resultcode;
			trace("resultcode=" + resultcode);
			var errormsg:String=json.errormsg;
			trace("errormsg=" + errormsg);
			if (resultcode == "0")
			{
				if (_model.wordAc != null)
				{
					_model.wordAc.removeAll();
				}
				var words:Object=json.words;
				for (var key:Object in words)
				{
					var wordid:String=words[key].wordid;
					var curword:String=words[key].curword;
					var symbols:String=words[key].symbols;
					var resword:String=words[key].resword;
					var pictureurl:String=words[key].pictureurl;
					var soundurl:String=words[key].soundurl;
					var pos:String=words[key].pos;
					var wordprogress:String=words[key].wordprogress;
					var studytime:String=words[key].studytime;
					var word:WordVo=new WordVo();
					word.wordid=wordid;
					word.curword=curword;
					word.symbols=symbols;
					word.resword=resword;
					word.pictureurl=pictureurl;
					word.soundurl=soundurl;
					word.pos=pos;
					word.wordprogress=wordprogress;
					word.studytime=studytime;
					var sentences:Object=words[key].sentences;
					for (var k:Object in sentences)
					{
						var sentenceid:String=sentences[k].sentenceid;
						var cursentence:String=sentences[k].cursentence;
						var ressentence:String=sentences[k].ressentence;
						var pinyin:String=sentences[k].pinyin;
						var sentencepic:String=sentences[k].pictureurl;
						var sentencesound:String=sentences[k].soundurl;
						if (sentenceid != null && sentenceid != "")
						{
							var sentence:SentenceVo=new SentenceVo();
							sentence.sentenceid=sentenceid;
							sentence.cursentence=cursentence;
							sentence.ressentence=ressentence;
							sentence.pinyin=pinyin;
							sentence.sentencepic=sentencepic;
							sentence.sentencesound=sentencesound;
							if (word.sentences == null)
							{
								word.sentences=new ArrayCollection();
							}
							word.sentences.addItem(sentence);
						}
					}
					var exercisewords:Object=json.exercise;
					var count:int=0;
					if (word.exercises == null)
					{
						word.exercises=new ArrayCollection();
					}
					for (var m:Object in exercisewords)
					{
						var exerciseid:String=exercisewords[m].wordid;
						var exercisecurword:String=exercisewords[m].curword;
						var exercisesymbols:String=exercisewords[m].symbols;
						var exerciseresword:String=exercisewords[m].resword;
						var exercisepictureurl:String=exercisewords[m].pictureurl;
						var exercisesoundurl:String=exercisewords[m].soundurl;
						var exercisepos:String=exercisewords[m].pos;
//						trace("测试单词：" + exerciseid + "、" + exerciseresword);
						var exerciseword:WordVo=new WordVo();
						exerciseword.wordid=exerciseid;
						exerciseword.curword=exercisecurword;
						exerciseword.resword=exerciseresword;
						exerciseword.symbols=exercisesymbols;
						exerciseword.pictureurl=exercisepictureurl;
						exerciseword.soundurl=exercisesoundurl;
						exerciseword.pos=exercisepos;
						if (count < Number(_model.wordInitParams.num) - 1)
						{
							word.exercises.addItem(exerciseword);
							count++;
						}
						else
						{
							count=0;
							var random:Number=int(Math.random() * Number(_model.wordInitParams.num));
//							trace("random==" + random);
							word.exercises.addItemAt(word, random);
							break;
						}
					}
					if (_model.wordAc == null)
					{
						_model.wordAc=new ArrayCollection();
					}
					_model.wordAc.addItem(word);
				}
				_model.curState="WordPreviewState"; //进入词汇预览界面
			}
			else
			{
				Alert.show(errormsg);
			}
		}

		public function fault(info:Object):void
		{
			Alert.show(FaultEvent(info).toString());
		}
	}
}